package cc.sheji.controller.admin;

import cc.sheji.Service.OrderService;
import cc.sheji.domain.JsonResult;
import cc.sheji.domain.Order;
import cc.sheji.domain.OrderQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Validated
@RestController

@RequestMapping(value = "/admin/v1/order")
public class AdminOrderController {

    @Autowired
    OrderService orderService;

    @RequestMapping("/{id}")
    public JsonResult get(@PathVariable("id") Long id) throws Exception {
        return JsonResult.Success(orderService.get(id));
    }

    @PostMapping()
    public JsonResult create(@RequestBody @Validated Order entity) {
        entity.setId(null);
        return JsonResult.Success(orderService.create(entity));
    }

    @PutMapping("/{id}")
    public JsonResult update(@PathVariable(name = "id") Long id, @Valid Order entity) {
        entity.setId(id);
        return JsonResult.Success(orderService.update(entity));
    }

    @GetMapping()
    public JsonResult get(OrderQueryCriteria criteria , Pageable pageable) throws InterruptedException {
        System.out.println(criteria);
        return JsonResult.Success(orderService.list(criteria,pageable));
    }

    @GetMapping("/all")
    public JsonResult getAll(OrderQueryCriteria criteria ) throws InterruptedException {
        return JsonResult.Success(orderService.getAll(criteria));
    }

}
